package com.alibaba.shopping_goods_service.service;

import com.alibaba.shopping_common.pojo.*;
import com.alibaba.shopping_common.service.GoodsService;
import com.alibaba.shopping_goods_service.mapper.GoodsImagesMapper;
import com.alibaba.shopping_goods_service.mapper.GoodsMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName GoodsServiceImpl
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/10 14:31
 */
@Transactional // 开启事务管理
@DubboService // 开启dubbo服务
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsImagesMapper goodsImagesMapper;


    // rocketMQ的工具类
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    // 商品信息变更topic
    private final String SYNC_GOOD_QUEUE = "sync_goods_queue";
    // 删除商品信息topic
    private final String DEL_GOOD_QUEUE = "del_goods_queue";
    // 更新购物车数据topic
    private final String SYNC_CART_QUEUE = "sync_cart_queue";
    // 删除购物车数据topic
    private final String DEL_CART_QUEUE = "del_cart_queue";

    @Override
    public void add(Goods goods) {
        // 插入商品数据
        goodsMapper.insert(goods);
        // 插入图片数据
        Long goodsId = goods.getId();
        List<GoodsImage> images = goods.getImages();
        for (GoodsImage image : images) {
            // 给图片设置商品ID
            image.setGoodsId(goodsId);
            goodsImagesMapper.insert(image);
        }

        // 插入商品_规格项数据
        List<Specification> specifications = goods.getSpecifications();
        // 创建规格项的list
        List<SpecificationOption> options = new ArrayList<>();
        for (Specification specification : specifications) {
            options.addAll(specification.getSpecificationOptions());
        }

        // 遍历规格项，插入商品规格项数据
        for (SpecificationOption option : options) {
            goodsMapper.addGoodsSpecificationOption(goodsId, option.getId());
        }
        // 将商品数据同步到ES中，保证数据一致性
        GoodsDesc goodsDesc = findDescById(goodsId);
        /*
         * 发送商品消息变更新的消息
         * SYNC_GOOD_QUEUE：商品信息变更的topic
         * goodsDesc：商品详情信息
         */
        rocketMQTemplate.syncSend(SYNC_GOOD_QUEUE,goodsDesc);
    }

    @Override
    public void update(Goods goods) {
        // 删除往期的图片数据
        Long goodsId = goods.getId();
        QueryWrapper<GoodsImage> wrapper = new QueryWrapper<>();
        wrapper.eq("goodsId",goodsId);
        goodsImagesMapper.delete(wrapper);
        // 删除往期的规格数据
        goodsMapper.deleteGoodsSpecificationOption(goodsId);

        // 修改商品数据
        goodsMapper.updateById(goods);

        // 插入图片数据
        List<GoodsImage> images = goods.getImages();
        for (GoodsImage image : images) {
            // 给图片设置商品ID
            image.setGoodsId(goodsId);
            goodsImagesMapper.insert(image);
        }

        // 插入商品_规格项数据
        List<Specification> specifications = goods.getSpecifications();
        // 创建规格项的list
        List<SpecificationOption> options = new ArrayList<>();
        for (Specification specification : specifications) {
            options.addAll(specification.getSpecificationOptions());
        }

        // 遍历规格项，插入商品规格项数据
        for (SpecificationOption option : options) {
            goodsMapper.addGoodsSpecificationOption(goodsId, option.getId());
        }
        // 将商品数据同步到ES中，保证数据一致性
        GoodsDesc goodsDesc = findDescById(goodsId);
        // searchService.syncGoodsToES(goodsDesc);
        /*
         * 发送商品消息变更新的消息
         * SYNC_GOOD_QUEUE：商品信息变更的topic
         * goodsDesc：商品详情信息
         */
        rocketMQTemplate.syncSend(SYNC_GOOD_QUEUE,goodsDesc);

        CartGoods cartGoods = new CartGoods();
        cartGoods.setGoodId(goods.getId());
        cartGoods.setGoodsName(goods.getGoodsName());
        cartGoods.setHeaderPic(goods.getHeaderPic());
        cartGoods.setPrice(goods.getPrice());
        // 发送更新购物车数据的消息
        rocketMQTemplate.syncSend(SYNC_CART_QUEUE,cartGoods);
    }

    @Override
    public Goods findById(Long id) {
        Goods goods = goodsMapper.findById(id);
        System.out.println("goods:"+goods);
        return goods;
    }

    @Override
    public void putAway(Long id, Boolean isMarketable) {
        goodsMapper.putAway(id, isMarketable);
        if (isMarketable){
            // 如果是上架操作，就将商品数据同步到ES中，保证数据一致性
            GoodsDesc goodsDesc = findDescById(id);
            System.out.println("上架操作,goodsDesc:"+goodsDesc);
            // searchService.syncGoodsToES(goodsDesc);
            /*
             * 发送商品变更新的消息
             * SYNC_GOOD_QUEUE：商品信息变更的topic
             * goodsDesc：商品详情信息
             */
            rocketMQTemplate.syncSend(SYNC_GOOD_QUEUE,goodsDesc);
        }else {
            // 如果是下架操作，就将ES中的商品数据删除，保证数据一致性
            /*
             * 发送商品删除的消息
             * id：商品ID
             * DEL_GOOD_QUEUE：商品消息删除的消息Topic
             */
            SendResult result = rocketMQTemplate.syncSend(DEL_GOOD_QUEUE, id);
            System.out.println(result);
            // 发送删除购物车商品数据的消息
            rocketMQTemplate.syncSend(DEL_CART_QUEUE,id);
        }
    }

    /**
     * 分页查询
     * @param goods 查询条件
     * @param page 页码
     * @param size 每页条数
     * @return
     */
    @Override
    public Page<Goods> search(Goods goods, int page, int size) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        if (goods != null && StringUtils.hasText(goods.getGoodsName())){
            wrapper.like("goodsName",goods.getGoodsName());
        }
        return goodsMapper.selectPage(new Page<>(page,size),wrapper);
    }


    /**
     * 查询所有的商品详情数据
     * @return
     */
    @Override
    public List<GoodsDesc> findAll() {
        return goodsMapper.findAll();
    }

    /**
     * 根据ID查询商品详情的数据(用户前端用户查询)
     * @param id
     * @return
     */
    @Override
    public GoodsDesc findDescById(Long id) {
        return goodsMapper.findDesc(id);
    }

    /**
     * 根据ID删除商品
     * @param id 商品ID
     */
    @Override
    public void delete(Long id) {
        // 删除bz_goods表中的数据
        goodsMapper.deleteById(id);
        // 删除bz_goods_images表中的数据
        QueryWrapper<GoodsImage> wrapper = new QueryWrapper<>();
        wrapper.eq("goodsId",id);
        goodsImagesMapper.delete(wrapper);
    }
}




